//
//  THJPortDataVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/7/5.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJPortDataVC.h"

@interface THJPortDataVC ()
{
    BOOL btn1Sel;
    BOOL btn2Sel;
    NSMutableArray * _bigpriceArr;
    NSMutableArray * _bigpriceArr2;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet PYZoomEchartsView *charView;
@property (nonatomic,strong)NSArray * dateArr;
@property (nonatomic,strong)NSMutableArray * nameArr;
@property (nonatomic,strong)NSArray * dateArr2;
@property (nonatomic,strong)NSMutableArray * nameArr2;
@end

@implementation THJPortDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:self.pageTitle];
    }else{
        [self setNavBarWithTitle:self.pageTitle];
    }
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = COLOR(34, 34, 34, 1);
    self.topSpace.constant = NavbarHeight;
    [self loadDataWithTypeId:@"1"];
    btn1Sel = YES;
    btn2Sel = NO;
    if([self.yDistance  isEqual: @(55)]){
        self.unitLab.text = @"单位：元/度";
        [self.btn1 setTitle:@"硅锰电价" forState:UIControlStateNormal];
        [self.btn2 setTitle:@"锰铁电价" forState:UIControlStateNormal];
    }else if([self.yDistance  isEqual: @(95)]){
        self.unitLab.text = @"单位：元/吨度";
    }
  
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadDataWithTypeId:(NSString*)typeId{
//    NSString * url = [NSString stringWithFormat:@"%@%@",self.url,typeId];
    __weak typeof(self)weakself = self;
    NSLog(@"港口url-%@",self.url);
    [MBProgressHUD showMessage:@"" toView:self.view];
    [THJNetWork getNetDataFromUrl:self.url callback:^(NSDictionary* receiveData, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        weakself.dateArr = receiveData[@"data"][@"1"][@"timedata"];
        NSArray * arr = receiveData[@"data"][@"1"][@"list"];
        _bigpriceArr = [NSMutableArray array];
        for (NSDictionary * dic in arr ) {
            NSLog(@"bigdata--%@",dic);
            NSMutableArray * priceArr = [NSMutableArray array];
            for (NSString * price in dic[@"dataList"]) {
                if ([price floatValue] == 0) {
                    [priceArr addObject:[NSNull null]];
                }else{
                    [priceArr addObject:[NSNumber numberWithFloat:[price floatValue]]];
                }
            }
            [_bigpriceArr addObject:priceArr];
        }
        _nameArr =  receiveData[@"data"][@"1"][@"typeList"];
        [weakself setChartViewDataWithDataArr:_bigpriceArr DateArr:weakself.dateArr];
        
        
        weakself.dateArr2 = receiveData[@"data"][@"2"][@"timedata"];
        NSArray * arr2 = receiveData[@"data"][@"2"][@"list"];
        _bigpriceArr2 = [NSMutableArray array];
        for (NSDictionary * dic in arr2 ) {
            NSLog(@"bigdata--%@",dic);
            NSMutableArray * priceArr = [NSMutableArray array];
            for (NSString * price in dic[@"dataList"]) {
                if ([price floatValue] == 0) {
                    [priceArr addObject:[NSNull null]];
                }else{
                    [priceArr addObject:[NSNumber numberWithFloat:[price floatValue]]];
                }
            }
            [_bigpriceArr2 addObject:priceArr];
        }
        _nameArr2 =  receiveData[@"data"][@"2"][@"typeList"];
        
    }];
}
-(void)setChartViewDataWithDataArr:(NSMutableArray*)dataArr DateArr:(NSArray*)dateArr {
    [_charView setOption:[self standardLineOptionWithArr1:dataArr DateArr:dateArr]];
    [_charView loadEcharts];
}
//天津港
- (IBAction)btn1Clicked:(UIButton*)sender {
   
    if (!btn1Sel) {
        [self.btn2 setBackgroundColor:COLOR(52, 52, 52, 1)];
        [sender setBackgroundColor:COLOR(34, 34, 34, 1)];
        [self setChartViewDataWithDataArr:_bigpriceArr DateArr:self.dateArr];
    }
    btn1Sel = YES;
    btn2Sel = NO;
    
}
//钦州港
- (IBAction)btn2Clicked:(UIButton*)sender {
    if(!btn2Sel){
        [self.btn1 setBackgroundColor:COLOR(52, 52, 52, 1)];
        [sender setBackgroundColor:COLOR(34, 34, 34, 1)];
        [self setChartViewDataWithDataArr:_bigpriceArr2 DateArr:self.dateArr2];
    }
    btn1Sel = NO;
    btn2Sel = YES;
}
- (PYOption *)standardLineOptionWithArr1:(NSArray*)arr1 DateArr:(NSArray*)dateArr{
    
    __weak typeof(self) weakself = self;
    return [PYOption initPYOptionWithBlock:^(PYOption *option) {
        option
        .tooltipEqual([PYTooltip initPYTooltipWithBlock:^(PYTooltip *tooltip) {
            tooltip.triggerEqual(PYTooltipTriggerAxis);
        }])
        .gridEqual([PYGrid initPYGridWithBlock:^(PYGrid *grid) {
            grid.xEqual(@40).x2Equal(@20);
            grid.yEqual(weakself.yDistance).y2Equal(@30);
        }])
        .legendEqual([PYLegend initPYLegendWithBlock:^(PYLegend *legend) {
            legend.dataEqual(_nameArr);
            legend.textStyle.colorEqual(@"#ffffff");
        }])
        .addXAxis([PYAxis initPYAxisWithBlock:^(PYAxis *axis) {
            axis.typeEqual(PYAxisTypeCategory)
            .boundaryGapEqual(@NO)
            .addDataArr(dateArr);
            axis.axisLabel.textStyleEqual([PYTextStyle initPYTextStyleWithBlock:^(PYTextStyle *textStyle) {
                textStyle.colorEqual(@"#ffffff");
            }]);
        }])
        .addYAxis([PYAxis initPYAxisWithBlock:^(PYAxis *axis) {
            axis.typeEqual(PYAxisTypeValue);
            axis.axisLabel.textStyleEqual([PYTextStyle initPYTextStyleWithBlock:^(PYTextStyle *textStyle) {
                textStyle.colorEqual(@"#ffffff");
            }]);
        }]);
        for (NSInteger i = 0;i< arr1.count;i++) {
            option.addSeries([PYSeries initPYSeriesWithBlock:^(PYSeries *series) {
                series.nameEqual(_nameArr[i])
                .typeEqual(PYSeriesTypeLine)
                .dataEqual(arr1[i]);
            }]);
        }
    }];
}

@end
